/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package automatolfa;

import java.util.ArrayList;
import java.util.Vector;

/**
 *
 * @author IVAN
 */
public class lfa2Main extends javax.swing.JFrame {

    /**
     * Creates new form lfa2Main
     */
    public lfa2Main() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtCodigo = new javax.swing.JTextArea();
        bntAnalisadorLexico = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstAnaliseLexica = new javax.swing.JList();
        btnAnalisadorSintatico = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstAnaliseSintatica = new javax.swing.JList();
        lblRespostaSintatico = new javax.swing.JLabel();
        lblRespostaLexico = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtCodigo.setColumns(20);
        txtCodigo.setRows(5);
        jScrollPane1.setViewportView(txtCodigo);

        bntAnalisadorLexico.setText("Léxico");
        bntAnalisadorLexico.setActionCommand("Analise Léxica");
        bntAnalisadorLexico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAnalisadorLexicoActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(lstAnaliseLexica);

        btnAnalisadorSintatico.setText("Sintatico");
        btnAnalisadorSintatico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnalisadorSintaticoActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(lstAnaliseSintatica);

        lblRespostaSintatico.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N

        lblRespostaLexico.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                    .addComponent(bntAnalisadorLexico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAnalisadorSintatico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3)
                    .addComponent(lblRespostaSintatico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblRespostaLexico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(bntAnalisadorLexico, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRespostaLexico, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23)
                        .addComponent(btnAnalisadorSintatico, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRespostaSintatico, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 866, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 597, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntAnalisadorLexicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntAnalisadorLexicoActionPerformed
        AnaliseLexica();
    }//GEN-LAST:event_bntAnalisadorLexicoActionPerformed

    private void btnAnalisadorSintaticoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnalisadorSintaticoActionPerformed
        AnaliseSintatica();
    }//GEN-LAST:event_btnAnalisadorSintaticoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(lfa2Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(lfa2Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(lfa2Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(lfa2Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new lfa2Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntAnalisadorLexico;
    private javax.swing.JButton btnAnalisadorSintatico;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblRespostaLexico;
    private javax.swing.JLabel lblRespostaSintatico;
    private javax.swing.JList lstAnaliseLexica;
    private javax.swing.JList lstAnaliseSintatica;
    private javax.swing.JTextArea txtCodigo;
    // End of variables declaration//GEN-END:variables

    private void AnaliseLexica() {
        
        boolean retorno;
        String textoRetorno;
        String codigo = this.txtCodigo.getText().replace("\n", "").replace("\t", "");
        AnalisadorLexico an =  new AnalisadorLexico(codigo);
        an.InsertAllAutomatos();        
        
        ArrayList<String> lstTokens = an.getLstToken();       
        retorno = an.Analisar();
        textoRetorno = (retorno ? "Análise léxica Ok!": "Análise léxica com erro");
        lblRespostaLexico.setText(textoRetorno);    
        
        lstAnaliseLexica.setListData(new Vector(lstTokens));
    }

    private void AnaliseSintatica() {
        boolean controle = true;
        String codigo = this.txtCodigo.getText().replace("\n", "").replace("\t", "");;
        AnalisadorLexico an =  new AnalisadorLexico(codigo);
        an.InsertAllAutomatos();
        an.Analisar();
        ArrayList<String> lstTokens = an.getLstToken();
        //AnalisadorSintatico as = new AnalisadorSintatico();
        AnalisadorSintatico as = new AnalisadorSintatico();
        controle = as.Analisar(lstTokens);
        ArrayList<String> lstAnalise = as.getLstAnalise();
        lstAnaliseSintatica.setListData(new Vector(lstAnalise));
        String textoRetorno;
        textoRetorno = (controle ? "Análise sintática Ok!": "Análise sintática com erro");
        lblRespostaSintatico.setText(textoRetorno);    
    }
}
